home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Borland / Borland C++ V5.02 / STDLIB.PAK / FOR_EACH.CPP < prev    next >
C/C++ Source or Header  |  1997-05-06  |  692b  |  35 lines

  1.  #include <vector>
  2.  #include <algorithm>
  3.  
  4.  using namespace std;
  5.  
  6.  //
  7.  // Function class that outputs its argument times x.
  8.  //
  9.  template <class Arg>
  10.  class out_times_x :  private unary_function<Arg,void>
  11.  {
  12.    private:
  13.       Arg multiplier;
  14.    public:
  15.       out_times_x(const Arg& x) : multiplier(x) { }
  16.       void operator()(const Arg& x) { cout << x * multiplier << " " << endl; }
  17.  };
  18.  
  19.  int main ()
  20.  {
  21.    int sequence[5] = {1,2,3,4,5};
  22.    //
  23.    // Set up a vector.
  24.    //
  25.    vector<int> v(sequence+0, sequence+5);
  26.    //
  27.    // Setup a function object.
  28.    //
  29.    out_times_x<int> f2(2);
  30.  
  31.    for_each(v.begin(),v.end(),f2);   // Apply function
  32.  
  33.    return 0;
  34.  }
  35.